package com.qianfeng.smartmetting.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianfeng.smartmetting.authiority.RequirePermission;
import com.qianfeng.smartmetting.entity.Booking;
import com.qianfeng.smartmetting.entity.TimeSlot;
import com.qianfeng.smartmetting.entity.dto.BookingDTO;
import com.qianfeng.smartmetting.entity.dto.BookingStateDTO;
import com.qianfeng.smartmetting.entity.vo.MyBooking;
import com.qianfeng.smartmetting.service.BookingService;
import com.qianfeng.smartmetting.service.TimeSlotService;
import com.qianfeng.smartmetting.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/booking")
public class BookingController {

    @Autowired
    private BookingService bookingService;
    @Autowired
    private TimeSlotService timeSlotService;


    //默认角色前缀 ROLE_
    @RequirePermission("user:list")
    @GetMapping("/list")
    public Map list(int page, int pagesize, HttpServletRequest request){
        try{
            Page<Booking> page1 = new Page<>(page,pagesize);
            IPage<Booking> obj = bookingService.page(page1);
            return CommonResult.ok("查询成功",obj);
        }catch (Exception e){
            return CommonResult.error("服务器异常");
        }
    }
    @PostMapping("/add")
    public Map add(@RequestBody BookingDTO bookingDTO){
        try{
            boolean flag = bookingService.svaeBooking(bookingDTO);
            return CommonResult.ok(flag?"预约成功":"预约失败",null);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("服务器异常");
        }
    }
    @PostMapping("/modify")
    public Map updateState(int id,int state){
        try{
            //修改数据使用UpdateWrapper update booking set state = state where id = id
            UpdateWrapper<Booking> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("state",state);
            updateWrapper.eq("id",id);
            boolean flag = bookingService.update(updateWrapper);
            return CommonResult.ok(flag?"修改成功":"修改失败",null);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.error("服务器异常");
        }
    }

    //我的预约

    @GetMapping("/myBooking/{id}")
    public Map myBooking(@PathVariable(name = "id") int id){
        try{
            List<MyBooking> list = bookingService.getMyBooking(id);
            return CommonResult.ok("查询成功",list);
        }catch (Exception e){
            return CommonResult.error("服务器异常");
        }
    }


    //更改预约状态
    @PostMapping("/updateState")
    public Map updateState(@RequestBody BookingStateDTO bookingStateDTO){
        try{
            UpdateWrapper<Booking> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("state",bookingStateDTO.getState());
            updateWrapper.eq("id",bookingStateDTO.getId());
            boolean flag = bookingService.update(updateWrapper);
            return CommonResult.ok(flag?"修改成功":"修改失败",null);
        }catch (Exception e){
            return CommonResult.error("服务器异常");
        }
    }

    //判断当前会议室当前时间是否可以被预约
    @GetMapping("/isBooking")
    public Map isBooking(Integer roomid,String starttime,String endtime){
        try{
            Integer count = bookingService.isBooking(roomid,starttime,endtime);
            return CommonResult.ok(count==0?"可以预约":"不可以预约",null);
        }catch (Exception e){
            return CommonResult.error("服务器异常");
        }
    }


}
